package com.blt.car.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blt.car.R;


public class CommonTitleBar extends RelativeLayout {
    private View rootView;

    private ImageView leftImage;

    private TextView middleTitle;

    private TextView rightTextView;

    public CommonTitleBar(Context context) {
        this(context, null);
    }

    public CommonTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.common_title_bar, this, true);
        rootView = findViewById(R.id.title_bar_root_rl);
        leftImage = (ImageView) findViewById(R.id.title_bar_back_btn);
        middleTitle = (TextView) findViewById(R.id.title_bar_title_txt);
        rightTextView = (TextView) findViewById(R.id.title_bar_right_txt);
    }

    public void setLeftImageClickListener(OnClickListener onClickListener) {
        leftImage.setOnClickListener(onClickListener);
    }

    public void setTitle(int resId) {
        middleTitle.setText(getContext().getString(resId));
    }

    public void setTitle(String title) {
        middleTitle.setText(title);
    }

    public void setRightText(String rightText) {
        rightTextView.setText(rightText);
    }

    public void hideRight() {
        if (rightTextView != null) {
            rightTextView.setVisibility(View.GONE);
        }
    }

    public void showRight(){
        rightTextView.setVisibility(View.VISIBLE);
    }

    public void setRightTextClickListener(OnClickListener onClickListener) {
        rightTextView.setOnClickListener(onClickListener);
    }

    @Override
    public void setBackgroundColor(int color) {
        rootView.setBackgroundColor(color);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void setBackground(Drawable background) {
        rootView.setBackground(background);
    }

    @Override
    public void setBackgroundResource(int resid) {
        rootView.setBackgroundResource(resid);
    }

    @Override
    public void setBackgroundDrawable(Drawable background) {
        rootView.setBackgroundDrawable(background);
    }

    public ImageView getLeftImage() {
        return leftImage;
    }

    public TextView getMiddleTitle() {
        return middleTitle;
    }

    public TextView getRightTextView() {
        return rightTextView;
    }
}
